//
//  HMVisitorView.swift
//  weibo
//
//  Created by LCH on 16/3/4.
//  Copyright © 2016年 lch. All rights reserved.
//

import UIKit


@objc protocol HMVisitorViewDelegate: NSObjectProtocol {

    optional func visitorView(visitorView:HMVisitorView, loginBtnDidClick:UIButton);


}



class HMVisitorView: UIView {
    
    weak var delegate: HMVisitorViewDelegate?

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
        
        startAnimation()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setPageInfo(imageName:String?, message:String?) {
    
        if imageName == nil {
        
        startAnimation()
        
        }else {
        
            iconView.image = UIImage(named: imageName!)
            circleView.hidden = true
            messageLabel.text = message
        
        }
    
    
    
    }
    
 
    private func startAnimation() {
    
    let anim = CABasicAnimation(keyPath: "transform.rotation")
    
    anim.toValue = 2 * M_PI
    
    anim.repeatCount = MAXFLOAT
    
    anim.duration = 20
        
    anim.removedOnCompletion = false
        
    circleView.layer.addAnimation(anim, forKey: nil)
    
    
    }
    
    
    
    private lazy var iconView: UIImageView = {
    
     let img = UIImageView(image:(UIImage(named: "visitordiscover_feed_image_house")))
        
     return img
    
    }()
    
    private lazy var circleView: UIImageView = UIImageView(image: (UIImage(named: "visitordiscover_feed_image_smallicon")))
    
    private lazy var messageLabel: UILabel = {
      
         let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
        
         label.numberOfLines = 2
        
        label.textAlignment = .Center
        
        label.text = "关注一些人，回这里看看有什么惊喜"
        return label
        
        
    }()
    
    private lazy var registerButton: UIButton = {
    
    let button = UIButton(textColor: UIColor.darkGrayColor(), fontSize: 14)
        
      button.setTitle("注册", forState: UIControlState.Normal)
        
     button.setBackgroundImage(UIImage(named: "common_button_white"), forState:UIControlState.Normal)
        
    button.addTarget(self, action: "loginButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
      return button
        
  }()
    
    private lazy var loginButton: UIButton = {
        let button = UIButton(textColor: UIColor.darkGrayColor(), fontSize: 14)
        button.setTitle("登录", forState: UIControlState.Normal)
        // 设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "loginButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        return button;
        
        
        }()
    
    
    private lazy var maskIconView: UIImageView = UIImageView(image: (UIImage(named: "visitordiscover_feed_mask_smallicon")))
    
    @objc private func loginButtonClick(button:UIButton) {
    
        delegate?.visitorView?(self, loginBtnDidClick: button)
    
    
    
    }
    
    
    
    
   

    
    
}

extension  HMVisitorView{
    private func setupUI() {
        
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        circleView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        
    }
    
    
}

